using System;
using System.Collections.Generic;
using System.Linq;

namespace DIS_DAL_WS.Repositories
{
	#region using
	using DIS_DAL_WS.Models;
	using DIS_DAL_WS.Helpers;
	#endregion
	public class DailyReportRepository
	{
		public List<DailyReport> SelectAll()
		{
			try
			{
				return DataContext.Instance.DailyReport.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<DailyReport>();
		}

		public DailyReport SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.DailyReport.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(DailyReport obj)
		{
			try
			{
				DataContext.Instance.DailyReport.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.DailyReport.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.DailyReport.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<DailyReport> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.DailyReport where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<DailyReport>();
		}

		public List<DailyReport> SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			try
			{
				var lstItem = (from item in DataContext.Instance.DailyReport where item.IDChemistWareHouse == idChemistWareHouse select item).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<DailyReport>();
		}

        public DailyReport SelectByIDChemistWareHouseAndReportDate(int idChemistWareHouse, DateTime reportdate)
        {
            try
            {
                var lst = DataContext.Instance.DailyReport.ToList();
                foreach (var item in lst)
                {
                    if (item.IDChemistWareHouse == idChemistWareHouse && item.ReportDate.Value.Date == reportdate.Date)
                    {
                        return item;
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }
	}
}